<?php
class DeliveryModel extends Model {
    public function __construct() {
        parent::__construct();
        $this->check = new DeliveryCheck();
        $this->tables = array(DB_FREFIX.'delivery');
        $this->fields = array('id','name','url','info','date','price_in','price_out','price_add');
        list(
            $this->R['id'],
            $this->R['name']
        ) = $this->getRequest()->getParams(
            array(
               isset($_GET['id']) ? $_GET['id'] : null,
               isset($_POST['name']) ? $_POST['name'] : null
            )
        );
    }

    public function findOne() {
        $condition = array('id='.$this->R['id']);
        if(!$this->check->oneCheck($this,$condition)) $this->check->displayError();
        return parent::select($this->tables,array('id','name','url','info','price_in','price_out','price_add'),array('where'=>$condition,'limit'=>1));
    }


    public function findAll() {
        return parent::select($this->tables,array('id','name','info','url','price_in','price_out','price_add'),array('limit'=>$this->limit,'order'=>'date DESC'));
    }

    public function findUpdateOrder() {
        return parent::select($this->tables,array('id','name','url'));
    }

    public function findFlowDelivery() {
        return parent::select($this->tables,array('id','name','info','url','price_in','price_out','price_add'));
    }

    public function add() {
        $condition = array('name='.$this->R['name']);
        if(!$this->check->addCheck($this,$condition)) $this->check->displayError();
        $needData = $this->getRequest()->filter($this->fields);
        $needData['date'] = Tool::getDate();
        return parent::add($needData);
    }

    public function update() {
        $condition = array('id='.$this->R['id']);
        if(!$this->check->oneCheck($this, $condition)) $this->check->displayError();
        $updateData = $this->getRequest()->filter($this->fields);
        if(!$this->check->updateCheck()) $this->check->displayError();
        return parent::update($updateData,$condition);
    }

    public function delete() {
        $condition = array('id='.$this->R['id']);
        if(!$this->check->oneCheck($this,$condition)) $this->check->displayError();
        return parent::delete($condition);
    }

    public function ajaxCheckName() {
        $condition = array('name='.$this->R['name']);
        $this->check->ajaxCheckName($this,$condition);
    }

    public function total() {
        return parent::total();
    }

}




?>